#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: kang.cunhua@qq.com
# @Date:   2016-05-27 14:40:47
# @Last Modified by:   kang.cunhua@qq.com
# @Last Modified time: 2016-06-30 18:50:14
from __future__ import unicode_literals
from flask import render_template, request, current_app
from app.models import db
from . import main
import app.utils

"""cmdb所有请求的路由定义
"""

# bc_nav_profile = current_app.config.get("BREADCRUMB_NAV_PROFILE")
# bc_nav_itms = current_app.config.get("BREADCRUMB_NAV_ITMS")


@main.route("/resources/idc/", methods=["GET"])
def resources_idc():
    """IDC 列表页面

    Args:
        idc_id (TYPE): Description

    Returns:
        TYPE: Description
    """

    bc_nav = [('/', 'dream', '中国梦'), ('/resources/idc/', 'resources', 'IT资产'),
              ('/resources/idc/', 'resource_idc', 'IDC管理')]

    ret = app.utils.api_action("idc.get", {"where": {"status": 1}})
    return render_template("resources/server_idc_list.html",
                           title="IDC信息",
                           show_resource=True,
                           show_idc_list=True,
                           bc_nav=bc_nav,
                           active_page="resource_idc",
                           idcs=ret)


@main.route("/resources/idc/modify/<int:idc_id>", methods=["GET"])
def resources_idc_modify(idc_id):
    """修改IDC信息

    Args:
        idc_id (TYPE): Description

    Returns:
        TYPE: Description
    """
    bc_nav = [('/', 'dream', '中国梦'), ('/resources/idc/', 'resources', 'IT资产'),
              ('/resources/idc/', 'resource_idc', 'IDC管理'), ('/resources/idc/modify/<int:idc_id>', 'resource_idc_modify', 'IDC编辑')]
    ret = app.utils.api_action("idc.get", {"where": {"id": idc_id}})
    if ret:
        return render_template("resources/server_idc_modify.html",
                               title="修改IDC信息",
                               show_resource=True,
                               show_idc_list=True,
                               show_idc_modify=True,
                               bc_nav=bc_nav,
                               active_page="resource_idc_modify",
                               idc=ret[0])
    return render_template("404.html"), 404


@main.route("/resources/idc/update", methods=["POST"])
def resources_idc_update():
    """更新IDC信息

    Args:
        idc_id (TYPE): Description

    Returns:
        TYPE: Description
    """
    data = request.form.to_dict()
    id = data.pop("id")
    ret = app.utils.api_action("idc.update", {"data": data, "where": {"id": id}})
    if ret:
        return render_template("public/success.html", next_url="/resources/idc/")
    else:
        return render_template("public/error.html", next_url="/resources/idc/")


@main.route("/resources/idc/add/", methods=["GET"])
def resources_add_idc():
    """添加IDC信息

    Args:
        idc_id (TYPE): Description

    Returns:
        TYPE: Description
    """
    bc_nav = [('/', 'dream', '中国梦'), ('/resources/idc/', 'resources', 'IT资产'),
              ('/resources/idc/', 'resource_idc', 'IDC管理'), ('/resources/idc/add/', 'resource_idc_add', 'IDC添加')]
    return render_template("resources/server_idc_add.html",
                           title="添加IDC",
                           show_resource=True,
                           show_idc_list=True,
                           show_idc_add=True,
                           bc_nav=bc_nav,
                           active_page="resource_idc_add")


@main.route("/resources/idc/doadd/", methods=["POST"])
def resources_doadd_idc():
    """执行添加IDC信息

    Args:
        idc_id (TYPE): Description

    Returns:
        TYPE: Description
    """
    params = request.form.to_dict()
    ret = app.utils.api_action("idc.create", params)
    if ret:
        return render_template("public/success.html", next_url="/resources/idc/", title="操作成功")
    else:
        return render_template("public/error.html", next_url="/resources/idc/", title="操作失败")


@main.route("/resources/idc/delete/", methods=["POST"])
def resources_idc_delete():
    """删除IDC信息：更新idc的状态为0"""
    id = request.form.get("id", 0)
    ret = app.utils.api_action("idc.update", {"where": {"id": id}, "data": {"status": 0}})
    return str(ret)
